package biz.inspeed.test.mina.android;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

/**
 * 用于接受请求的
 * 
 * @author NKY(ACNTWWW@GMAIL.COM) 2012-8-23
 */
public class MinaServer
{
	public static MinaServer instance = new MinaServer ();
	
	/***
	 * 获取Server的实例，用于单利模式
	 * @return
	 */
	public static MinaServer getInstance(){
		return instance;
	}
	private MinaServer()
	{
		startServer ();
	}

	/****
	 * 启动服务
	 */
	private void startServer ()
	{
		IoAcceptor acceptor = new NioSocketAcceptor ();
//		acceptor.getFilterChain ().addLast ( "codec" ,
//				new ProtocolCodecFilter ( new TextLineCodecFactory ( Charset.forName ( "UTF-8" ) ) ) );

		acceptor.setHandler ( new MinaIoHandler () );
		acceptor.getSessionConfig ().setReadBufferSize ( 2048 );
		acceptor.getSessionConfig ().setIdleTime ( IdleStatus.BOTH_IDLE , 20 );

		try
		{
			acceptor.bind ( new InetSocketAddress ( Constants.PORT ) );
		}
		catch ( IOException e )
		{
			e.printStackTrace();
		}
		System.out.println( "监听服务器启动");
	}
}
